<template>
  <div class="i-table-no-border">
    <div>
      <table-btns
        :tableTopOperateData="listData.tableTopOperate"
        :tableTopBatchOperate="listData.tableTopBatchOperate"
        :selectedData="selectedData"
        :requestApi="listData.requestApi"
        :basePath="basePath"
        @pSub="pSub"
        @exportData="exportData"
        @actionsChange="getData"
      >
      </table-btns>
    </div>
    <!-- 表格内容 start  -->
    <Table
      ref="table"
      :columns="columns"
      :data="tableDataList"
      :loading="loading"
      class="ivu-mt"
      stripe
      @on-filter-change="handleFilterChange"
      @on-select="handleSelect"
      @on-select-cancel="handleSelectCancel"
      @on-select-all="handleSelectAll"
      @on-select-all-cancel="handleSelectAllCancel"
    >
      <template slot-scope="{ row }" slot="compareArea">
        <span>{{ row.old.compareArea }}</span>
      </template>
      <template slot-scope="{ row }" slot="compareType">
        <span>{{ row.old.compareType }}</span>
      </template>
      <template slot-scope="{ row }" slot="compareTargetName">
        <span>{{ row.old.compareTargetName }}</span>
      </template>
      <template slot-scope="{ row }" slot="compareTarget">
        <span>{{ row.old.compareTarget }}</span>
      </template>
      <template slot-scope="{ row }" slot="compareMan">
        <span>{{ row.old.compareMan }}</span>
      </template>
      <template slot-scope="{ row }" slot="compareTel">
        <span>{{ row.old.compareTel }}</span>
      </template>
      <template slot-scope="{ row }" slot="compareAddress">
        <span>{{ row.old.compareAddress }}</span>
      </template>
      <template slot-scope="{ row }" slot="brandDiff">
        <span>{{ row.old.brandDiff }}</span>
      </template>
      <template slot-scope="{ row }" slot="compareRule">
        <span>{{ row.old.compareRule }}</span>
      </template>
      <template slot-scope="{ row }" slot="securityRateMin">
        <span>{{ row.old.securityRateMin }}</span>
      </template>
      <template slot-scope="{ row }" slot="securityPriceMin">
        <span>{{ row.old.securityPriceMin }}</span>
      </template>
      <template slot-scope="{ row }" slot="securityRateMax">
        <span>{{ row.old.securityRateMax }}</span>
      </template>
      <template slot-scope="{ row }" slot="securityPriceMax">
        <span>{{ row.old.securityPriceMax }}</span>
      </template>
      <template slot-scope="{ row, index }" slot="compareAreaUpdate">
        <Input
          v-model="row.compareAreaUpdate"
          @on-change="changInput(row, index)"
          placeholder="请输入..."
        />
      </template>
      <template slot-scope="{ row }" slot="compareType">
        <span v-if="row.old.compareType == '1'">一品多供</span>
        <span v-if="row.old.compareType == '2'">竞争对手</span>
        <span v-if="row.old.compareType == '3'">同品类供应商</span>
        <span v-if="row.old.compareType == '4'">同品牌供应商</span>
      </template>
      <template slot-scope="{ row, index }" slot="compareTypeUpdate">
        <Select
          v-model="row.compareTypeUpdate"
          style="width: 120px"
          @on-change="compareTypeSelect($event, row)"
        >
          <Option
            v-for="item in compareTypeUpdateList"
            :value="item.optionValue"
            :key="item.optionHtml"
          >
            {{ item.optionHtml }}
          </Option>
        </Select>
      </template>
      <template slot-scope="{ row, index }" slot="compareTargetNameUpdate">
        <div v-if="row.compareTypeUpdate === '2'">
          <Input
            v-model="row.compareTargetNameUpdate"
            @on-change="changInput(row, index)"
            placeholder="请输入..."
          />
        </div>
        <div v-else>
          <Input
            v-model="row.compareTargetNameUpdate"
            placeholder="请选择"
            enter-button
            readonly
            style="width: 120px"
          />
        </div>
      </template>
      <template slot-scope="{ row, index }" slot="compareTargetUpdate">
        <div v-if="row.compareTypeUpdate === '2'">
          <Input
            v-model="row.compareTargetUpdate"
            @on-change="changInput(row, index)"
            placeholder="请输入..."
          />
        </div>
        <div v-else>
          <Input
            v-model="row.compareTargetUpdate"
            placeholder="请选择"
            @on-search="compareFun(row, index)"
            search
            enter-button
            readonly
            style="width: 120px"
          />
        </div>
      </template>
      <template slot-scope="{ row, index }" slot="compareTelUpdate">
        <div v-if="row.compareTypeUpdate === '2'">
          <Input
            v-model="row.compareTelUpdate"
            @on-change="changInput(row, index)"
            placeholder="请输入..."
          />
        </div>
        <div v-else>
          <Input
            v-model="row.compareTelUpdate"
            disabled
            @on-change="changInput(row, index)"
            placeholder="请输入..."
          />
        </div>
      </template>
      <template slot-scope="{ row, index }" slot="compareManUpdate">
        <div v-if="row.compareTypeUpdate === '2'">
          <Input
            v-model="row.compareManUpdate"
            @on-change="changInput(row, index)"
            placeholder="请输入..."
          />
        </div>
        <div v-else>
          <Input
            v-model="row.compareManUpdate"
            disabled
            @on-change="changInput(row, index)"
            placeholder="请输入..."
          />
        </div>
      </template>
      <template slot-scope="{ row, index }" slot="compareAddressUpdate">
        <div v-if="row.compareTypeUpdate === '2'">
          <Input
            v-model="row.compareAddressUpdate"
            @on-change="changInput(row, index)"
            placeholder="请输入..."
          />
        </div>
        <div v-else>
          <Input
            v-model="row.compareAddressUpdate"
            disabled
            @on-change="changInput(row, index)"
            placeholder="请输入..."
          />
        </div>
      </template>
      <template slot-scope="{ row, index }" slot="brandDiffUpdate">
        <Input
          v-model="row.brandDiffUpdate"
          @on-change="changInput(row, index)"
          placeholder="请输入..."
        />
      </template>
      <template slot-scope="{ row, index }" slot="lastSaleCountUpdate">
        <Input
          v-model="row.lastSaleCountUpdate"
          @on-change="changInput(row, index)"
          placeholder="请输入..."
        />
      </template>
      <template slot-scope="{ row, index }" slot="nowSaleCountUpdate">
        <Input
          v-model="row.nowSaleCountUpdate"
          @on-change="changInput(row, index)"
          placeholder="请输入..."
        />
      </template>
      <template slot-scope="{ row, index }" slot="saleRateUpdate">
        <Input
          v-model="row.saleRateUpdate"
          @on-change="changInput(row, index)"
          placeholder="请输入..."
        />
      </template>
      <template slot-scope="{ row, index }" slot="compareRuleUpdate">
        <Input
          v-model="row.compareRuleUpdate"
          @on-change="changInput(row, index)"
          placeholder="请输入..."
        />
      </template>
      <template slot-scope="{ row, index }" slot="securityRateMaxUpdate">
        <Input
          v-model="row.securityRateMaxUpdate"
          @on-change="changInput(row, index)"
          placeholder="请输入..."
        />
      </template>
      <template slot-scope="{ row, index }" slot="securityRateMinUpdate">
        <Input
          v-model="row.securityRateMinUpdate"
          @on-change="changInput(row, index)"
          placeholder="请输入..."
        />
      </template>
      <template slot-scope="{ row, index }" slot="securityPriceMaxUpdate">
        <Input
          v-model="row.securityPriceMaxUpdate"
          @on-change="changInput(row, index)"
          placeholder="请输入..."
        />
      </template>
      <template slot-scope="{ row, index }" slot="securityPriceMinUpdate">
        <Input
          v-model="row.securityPriceMinUpdate"
          @on-change="changInput(row, index)"
          placeholder="请输入..."
        />
      </template>
      <template slot-scope="{}" slot="actions">
        <a href="#">关联</a>
      </template>
      <template slot-scope="{ row }" slot="action">
        <div v-if="row.actions && row.actions.length > 2">
          <a @click="handleClickMore(row.actions[0].name, row)">{{
            row.actions ? row.actions[0].label : ""
          }}</a>
          <Divider type="vertical" />
          <Dropdown @on-click="(name) => handleClickMore(name, row)" transfer>
            <a
              >更多
              <Icon type="ios-arrow-down" />
            </a>
            <DropdownMenu slot="list">
              <DropdownItem
                v-for="(item, i) in row.actions.slice(1)"
                :key="i"
                :name="item.name"
              >
                {{ item.label }}
              </DropdownItem>
            </DropdownMenu>
          </Dropdown>
        </div>
        <div v-if="row.actions && row.actions.length <= 2">
          <a
            v-if="row.actions"
            @click="handleClickMore(row.actions[0].name, row)"
            >{{ row.actions[0].label }}</a
          >
          <Divider
            v-if="row.actions && row.actions.length > 1"
            type="vertical"
          />
          <a
            v-if="row.actions && row.actions.length > 1"
            @click="handleClickMore(row.actions[1].name, row)"
            >{{ row.actions[1].label }}</a
          >
        </div>
      </template>
    </Table>
    <!-- 表格内容 end  -->
    <!-- 分页 start  -->
    <div class="ivu-mt ivu-text-center">
      <Page
        :total="totalNum"
        show-total
        show-sizer
        :current.sync="nowPage"
        @on-change="getData"
        @on-page-size-change="handleChangePage"
      />
    </div>
    <Modal v-model="compareModal" title="选择比价对象" width="800">
      <Row style="margin-top: 15px">
        <Table :columns="compareCol" :data="compareData">
          <template slot-scope="{ row }" slot="action">
            <Button
              type="success"
              size="small"
              style="margin: 3px 0"
              @click="compareOk(row)"
              >选择</Button
            >
          </template>
        </Table>
        <div class="ivu-mt ivu-text-center">
          <Page
            :total="totalNum"
            show-total
            show-sizer
            transfer
            :current.sync="nowPage"
            @on-change="linkAttributeChang()"
            @on-page-size-change="handleChangePage"
          />
        </div>
      </Row>
    </Modal>
    <!-- 分页 end  -->
  </div>
</template>
<script>
import { mapState, mapMutations } from 'vuex';
import util from '@/libs/util';
import { ApiRequest } from '@api/basePage';
import tables from '@/mixins/dynamictable'; // 混入通用代码

import qwInput from '@/components/common/formItem/qwInput'; // 表单input组件
import qwSelect from '@/components/common/formItem/qwSelect'; // 表单select组件
import qwDatePicker from '@/components/common/formItem/qwDatePicker'; // 表单日期组件
import qwSelectTree from '@/components/common/formItem/qwSelectTree'; // 表单下拉树组件
import tableBtns from './tableBtns'; // 表格内部操作按钮

export default {
  mixins: [tables],
  props: {
    dynamicData: {
      type: Object
    },
    pagePath: {
      type: String
    },
    orderState: {
      type: String,
      default: ''
    }
  },
  computed: {
    ...mapState('admin/baseList', [
      'listData',
      'searchFormData',
      'searchFormDataQ'
    ])
  },
  watch: {
    orderState (n, o) {
      this.getData();
    }
  },
  components: {
    qwInput,
    qwSelect,
    qwDatePicker,
    qwSelectTree,
    tableBtns
  },
  data () {
    return {
      actions: [],
      specTableData: [],
      markupTypeUpdateList: [
        {
          optionValue: '1',
          optionHtml: '金额'
        },
        {
          optionValue: '2',
          optionHtml: '比例'
        }
      ],
      comparePirceTypeUpdateList: [
        {
          optionValue: '1',
          optionHtml: '售价'
        },
        {
          optionValue: '2',
          optionHtml: '现金价'
        },
        {
          optionValue: '3',
          optionHtml: '分销价'
        }
      ],
      compareTypeUpdateList: [
        {
          optionValue: '1',
          optionHtml: '一品多供'
        },
        {
          optionValue: '2',
          optionHtml: '竞争对手'
        },
        {
          optionValue: '3',
          optionHtml: '同品类供应商'
        },
        {
          optionValue: '4',
          optionHtml: '同品牌供应商'
        }
      ],
      util: util,
      showTarget: false,
      basePath: this.dynamicData.basePath, // 拼接基础路径
      tableDataList: [],
      tableDataList1: [],
      dataOver2: [
        {
          compareTargetName: '123'
        }
      ],
      compareModal: false,
      selectIndex: 0,
      compareData: [],
      compareCol: [
        {
          title: '比价对象',
          key: 'compareTargetUpdate',
          align: 'center'
        },
        {
          title: '比价对象名字',
          key: 'compareTargetNameUpdate',
          align: 'center'
        },
        {
          title: '联系方式',
          key: 'compareTelUpdate',
          align: 'center'
        },
        {
          title: '地址',
          key: 'compareAddressUpdate',
          align: 'center'
        },
        {
          title: '操作',
          slot: 'action',
          align: 'center',
          fixed: 'right',
          minWidth: 120
        }
      ]
    };
  },
  created () {},
  methods: {
    ...mapMutations('admin/baseList', ['setTableRowData']),
    compareFun (row, index) {
      this.compareModal = true;
      this.selectIndex = index;
    },
    // 选中后处理
    compareOk (row) {
      this.tableDataList[this.selectIndex].compareTargetUpdate =
        row.compareTargetNameUpdate;
      this.tableDataList[this.selectIndex].compareTelUpdate =
        row.compareTelUpdate;
      this.tableDataList[this.selectIndex].compareAddressUpdate =
        row.compareAddressUpdate;
      this.tableDataList[this.selectIndex].compareTargetNameUpdate =
        row.compareTargetNameUpdate;
      this.tableDataList[this.selectIndex].compareManUpdate =
        row.compareManUpdate;
      this.compareModal = false;
    },
    compareTypeSelect (type, row) {
      let data = {};
      ApiRequest(
        '/sysback/purchasepriceruleupdate/getRuleTargetByType?' +
          'areaCode=' +
          row.areaCode +
          '&skuNo=' +
          row.skuNo +
          '&supplierName=' +
          row.supplierName +
          '&supplierType=' +
          row.supplierType +
          '&type=' +
          type,
        'get',
        data
      ).then((res) => {
        if (res.retStatus === '1') {
          this.compareData = res.retData;
        }
      });
    },
    // 获取表格的数据
    getData () {
      let searchParam = [];
      Object.keys(this.searchFormData).forEach((key) => {
        searchParam.push({
          name: key,
          value: this.searchFormData[key]
        });
      });
      Object.keys(this.searchFormDataQ).forEach((key) => {
        searchParam.push({
          name: key,
          value: this.searchFormDataQ[key]
        });
      });
      let data = {
        nowPage: this.nowPage,
        pageShow: this.pageShow,
        searchParam: JSON.stringify(searchParam),
        sortName: '',
        sortType: ''
      };
      this.loading = true;
      // 开始请求列表
      ApiRequest(
        this.listData.requestApi.getTableList.url,
        this.listData.requestApi.getTableList.type,
        data
      ).then((res) => {
        let rData = res;
        if (rData.retData !== '') {
          rData.retData = rData.retData;
        }
        let tableDataList = rData.retData.results;
        tableDataList.forEach((item) => {
          let actions = [];
          actions.push(
            {
              name: 'Edit',
              label: '修改',
              handleType: 'link'
            },
            {
              name: 'NewAdd',
              label: '增加比价对象',
              handleType: 'prop'
            }
          );
          item.actions = actions;
        });
        // 增加表格操作项
        this.tableDataList = rData.retData.results;
        this.tableDataList1 = rData.retData.results;
        this.totalNum = rData.retData.totalNum;
        this.loading = false;
      });
    },
    changInput (row, index) {
      this.tableDataList[index] = row;
      this.tableDataList1[index] = row;
    },
    // 批量提交按钮
    pSub () {
      let tableDataList2 = [];
      for (let i = 0; i < this.tableDataList1.length; i++) {
        if (!this.tableDataList1[i]._disabled) {
          let tableDataList1 = {};
          tableDataList1['comparePirceTypeUpdate'] =
            this.tableDataList1[i].comparePirceTypeUpdateName;
          tableDataList1['storeNameUpdate'] =
            this.tableDataList1[i].storeNameUpdate;
          tableDataList1['uuid'] = this.tableDataList1[i].uuid;
          tableDataList1['skuNo'] = this.tableDataList1[i].skuNo;
          tableDataList1['spuNo'] = this.tableDataList1[i].spuNo;
          tableDataList1['productUuid'] = this.tableDataList1[i].productUuid;
          tableDataList1['storeUuidUpdate'] =
            this.tableDataList1[i].storeUuidUpdate;
          tableDataList1['storeNoUpdate'] =
            this.tableDataList1[i].storeNoUpdate;
          tableDataList1['compareAreaUpdate'] =
            this.tableDataList1[i].compareArea;
          tableDataList1['compareTypeUpdate'] =
            this.tableDataList1[i].compareType;
          tableDataList1['compareTargetNameUpdate'] =
            this.tableDataList1[i].compareTargetName;
          tableDataList1['compareManUpdate'] =
            this.tableDataList1[i].compareMan;
          tableDataList1['compareTargetUpdate'] =
            this.tableDataList1[i].compareTarget;
          tableDataList1['compareTelUpdate'] =
            this.tableDataList1[i].compareTel;
          tableDataList1['compareAddressUpdate'] =
            this.tableDataList1[i].compareAddress;
          tableDataList1['brandDiffUpdate'] = this.tableDataList1[i].brandDiff;
          tableDataList1['lastSaleCountUpdate'] =
            this.tableDataList1[i].lastSaleCountUpdate;
          tableDataList1['nowSaleCountUpdate'] =
            this.tableDataList1[i].nowSaleCountUpdate;
          tableDataList1['saleRateUpdate'] = this.tableDataList1[i].saleRate;
          tableDataList1['compareRuleUpdate'] =
            this.tableDataList1[i].compareRule;
          tableDataList1['purchaseRateMaxUpdate'] =
            this.tableDataList1[i].purchaseRateMax;
          tableDataList1['purchaseRateMinUpdate'] =
            this.tableDataList1[i].purchaseRateMin;
          tableDataList1['purchasePriceMaxUpdate'] =
            this.tableDataList1[i].purchasePriceMax;
          tableDataList1['purchasePriceMinUpdate'] =
            this.tableDataList1[i].purchasePriceMin;
          tableDataList2.push(tableDataList1);
        }
      }
      if (tableDataList2.length > 0) {
        ApiRequest(
          '/sysback/salepriceruleupdate/batchUpdateRule',
          'post',
          tableDataList2
        ).then((res) => {
          if (res.retStatus == '1') {
            this.$Message.success('提交成功');
            this.getData();
          }
        });
      }
    },
    handleClickMore (name, row) {
      if (name === 'Delete') {
        this.$Modal.confirm({
          title: '删除操作',
          content: '确定删除此数据吗？',
          onOk: () => {
            let data = {
              uuid: row.uuid
            };
            // 开始请求商品列表
            ApiRequest(
              this.requestApi.delTableList.url,
              this.requestApi.delTableList.type,
              data
            ).then((res) => {
              this.$Message.success('删除成功');
              this.actionsChange();
            });
          }
        });
      } else if (name === 'Sub') {
        this.$Modal.confirm({
          title: '提交',
          content: '<p>是否要进行提交？</p>',
          onOk: () => {
            let that = this;
            let datas = {
              uuid: row.uuid,
              skuNo: row.skuNo,
              spuNo: row.spuNo,
              productUuid: row.productUuid,
              productName: row.productName,
              spec: row.spec,
              productManagerArea: row.productManagerArea,
              unit: row.unit,
              storeUuidUpdate: row.storeUuidUpdate,
              storeNoUpdate: row.storeNoUpdate,
              compareAreaUpdate: row.compareAreaUpdate,
              compareTypeUpdate: row.compareTypeUpdate,
              compareTargetNameUpdate: row.compareTargetNameUpdate,
              compareTargetUpdate: row.compareTargetUpdate,
              compareTelUpdate: row.compareTelUpdate,
              compareManUpdate: row.compareManUpdate,
              markupTypeUpdate: row.markupTypeUpdate,
              compareAddressUpdate: row.compareAddressUpdate,
              markupValueUpdate: row.markupValueUpdate,
              brandDiffUpdate: row.brandDiffUpdate,
              lastSaleCountUpdate: row.lastSaleCountUpdate,
              nowSaleCountUpdate: row.nowSaleCountUpdate,
              weighMarkupValueUpdate: row.weighMarkupValueUpdate,
              salePriceTypeUpdate: row.salePriceTypeUpdate,
              saleRateUpdate: row.saleRateUpdate,
              compareRuleUpdate: row.compareRuleUpdate,
              securityRateMinUpdate: row.securityRateMinUpdate,
              securityPriceMinUpdate: row.securityPriceMinUpdate,
              securityRateMaxUpdate: row.securityRateMaxUpdate,
              securityPriceMaxUpdate: row.securityPriceMaxUpdate
            };
            ApiRequest(
              this.listData.requestApi.subTableList.url,
              this.listData.requestApi.subTableList.type,
              datas
            ).then((res) => {
              if (res.retStatus == '1') {
                that.$Message.success('修改成功！');
                that.getData();
              }
            });
          },
          onCancel: () => {}
        });
      } else if (name === 'NewAdd') {
        let jumpPath = this.basePath + 'NewAdd';
        this.$router.push({ path: jumpPath, query: { row: row } });
      } else {
        let obj = {}; // 动作的对象信息
        row.actions.forEach((item) => {
          if (name === item.name) {
            obj = item;
            return false;
          }
        });
        this.setTableRowData(row);
        if (obj.handleType === 'link') {
          // 操作类型为链接时，拼接参数跳转
          let jumpPath = this.basePath + obj.name;
          this.$router.push({
            path: jumpPath,
            query: {
              uuid: row.uuid,
              orderType: row.orderType
            }
          });
        } else if (obj.handleType === 'prop') {
          // 操作类型为弹窗时 打开弹窗
          if (obj.name === 'viewing') {
            this.showPropviewing = true;
          } else if (obj.name === 'view') {
            this.showPropviewing = true;
          } else {
          }
        } else {
          // 操作类型为弹窗时 打开弹窗
        }
      }
    }
  }
};
</script>
<style lang="less" scoped>
.i-table-no-border .ivu-table-header th {
  background: #f8f8f8;
}
</style>
